<?php

/*
Info: Handles the real time notification updates
Author: Pritam Pebam
Version: 1.0
Contact: ptamzz@gmail.com; @ptamzz
URL: http://12bubbles.com
*/

	session_start();
	require_once('inc/fnc.php');
	require_once('inc/conn.php');
	$flag = 0;
	$markUp = '<div class="notWrap">
					<div class="topShadow"></div>
					<h1 class="title">Notifications</hg1>
					<ul id="userNot">';
	$query = "SELECT * FROM `notification` WHERE `followerid` = {$_SESSION['userId']} AND delflag = 'N' ORDER BY crtdtime DESC LIMIT 50";
	$result = mysql_query($query, $connection);			

	while($row = mysql_fetch_array($result)){
		$flag = 1;
		
		if($row['event']== 1) {
			$querys = "SELECT story.storyname as stline, entryline.entryline as entryl from entryline join story on entryline.storyid = story.storyid where entryline.displayseq = {$row['uniqueentryid']} and story.storyid = {$row['storyid']}";
			$results = mysql_query($querys,$connection);
			if(!$result) { echo "error"; }
			while($rows = mysql_fetch_array($results)) { $sttitle = $rows['stline']; $entryl = $rows['entryl']; }
			$querys = mysql_query("SELECT count(entrylineid) as cnt from entrylinevote where entrylineid = {$row['uniqueentryid']} and UNIX_TIMESTAMP(crtdtime) <= {$row['crtdtime']} and delflag = 'N' and vote = 1",$connection);
			while($rows = mysql_fetch_array($querys)) { $votecnt = $rows['cnt']; }
			$cnttyp = ($votecnt > 1) ? ' likes' : ' like';
			$btmline = 'The entry line has received '.$votecnt.$cnttyp;
		}
		if($row['event']== -1) {
			$querys = "SELECT story.storyname as stline, entryline.entryline as entryl from entryline join story on entryline.storyid = story.storyid where entryline.displayseq = {$row['uniqueentryid']} and story.storyid = {$row['storyid']}";
			$results = mysql_query($querys);
			while($rows = mysql_fetch_array($results)) { $sttitle = $rows['stline']; $entryl = $rows['entryl']; }
			$querys = mysql_query("SELECT count(entrylineid) as cnt from entrylinevote where entrylineid = {$row['uniqueentryid']} and UNIX_TIMESTAMP(crtdtime) <= {$row['crtdtime']} and delflag = 'N' and vote = -1");
			while($rows = mysql_fetch_array($querys)) { $votecnt = $rows['cnt']; }
			$cnttyp = ($votecnt > 1) ? ' likes' : ' like';
			$btmline = 'The entry line has received '.$votecnt.$cnttyp;
		}
		if($row['event']== 2) {
			$querys = "SELECT story.storyname as stline, storyline.storyline as entryl from storyline join story on storyline.storyid = story.storyid where storyline.storylineid = 1 and story.storyid = {$row['storyid']} and storyline.delflag = 'N'";
			$results = mysql_query($querys);
			while($rows = mysql_fetch_array($results)) { $sttitle = $rows['stline']; $entryl = $rows['entryl']; }
 			$btmline = 'A new story has been posted';
		}
		if($row['event']== 3) {
			$querys = "SELECT story.storyname as stline, entryline.entryline as entryl from entryline join story on entryline.storyid = story.storyid where entryline.displayseq = {$row['uniqueentryid']} and story.storyid = {$row['storyid']} and entryline.delflag = 'N'";
			$results = mysql_query($querys);
			while($rows = mysql_fetch_array($results)) { $sttitle = $rows['stline']; $entryl = $rows['entryl']; }
			$btmline = 'A new entry line has been posted.';
		}
		
		if($row['event']== 4) {
			$querys = "SELECT story.storyname as stline, entryline.entryline as entryl from entryline join story on entryline.storyid = story.storyid where entryline.displayseq = {$row['uniqueentryid']} and story.storyid = {$row['storyid']} and entryline.delflag = 'N'";
			$results = mysql_query($querys);
			while($rows = mysql_fetch_array($results)) { $sttitle = $rows['stline']; $entryl = $rows['entryl']; }
			$btmline = 'Congrats! Your entry line is now a story line.';
		}
		if($row['event']== 5) {
			$querys = "SELECT story.storyname as stline, entryline.entryline as entryl from entryline join story on entryline.storyid = story.storyid where entryline.displayseq = {$row['uniqueentryid']} and story.storyid = {$row['storyid']} and entryline.delflag = 'N'";
			$results = mysql_query($querys);
			while($rows = mysql_fetch_array($results)) { $sttitle = $rows['stline']; $entryl = $rows['entryl']; }
			$btmline = 'The entry line has become a story line.';
		}

		$markUp .= "<li class='eachNot'>
						<a href = '/story/{$row['storyid']}/" . getStoryTitle($row['storyid']) . "/" . $row['vote'] . "'>
							<div class='not'>
								<div class='notLine'><span>".$sttitle."</span> : ".$entryl."</div>
								<div class='notBtm'>".$btmline."</div>
							</div>
						</a>
					</li>";
	}
	$markUp .= '</ul>
				<br style="clear: both;" />
				<div class="bottomShadow"></div>
				<div>';
	if ($flag != 1){
		echo '<li class="eachNot">
					<div class="not">
						<h1>No Updates</h1>
						<div class="notLine">There are currently no new updates</div>
					</div>
				</li>';
	} else  {
		echo $markUp;
	}
?>